package com.brite.android.game.achievement.model.data.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.brite.android.game.achievement.model.data.connection.GetConnection;
import com.brite.android.game.achievement.model.orm.RankView;

public class RankViewDAO {
	
	private static final String DATABASE_NAME = "FIREBALL HUNTER";
	private static final int DATABASE_CURRENT_VERSION = 1;
//	private static final int DATABASE_NEW_VERSION = 2;
	private static final String TABLE_NAME = "tblRankings";
	
	private static SQLiteDatabase _database = null;
	private GetConnection connection;
	private Context context;
	
	public RankViewDAO(Context context){
		this.context = context;
	}
	
	public SQLiteDatabase getReadable(){
		connection = new GetConnection(context, DATABASE_NAME, null, DATABASE_CURRENT_VERSION);
		_database = connection.getReadableDatabase();
		return _database;
	}
	
	public SQLiteDatabase getWriteable(){
		connection = new GetConnection(context, DATABASE_NAME, null, DATABASE_CURRENT_VERSION);
		_database = connection.getWritableDatabase();
//		connection.onUpgrade(_database, DATABASE_CURRENT_VERSION, DATABASE_NEW_VERSION);
		return _database;
	}
	
	public void closeConnection(){
		connection.close();
	}
	
	public void insert(RankView rankView){

		ContentValues values = new ContentValues();
		values.put("userName", rankView.getUserName());
		values.put("score", rankView.getScore());
		values.put("numberKill", rankView.getNumberKill());
		
		_database.insert(TABLE_NAME, null, values);
	}
	
	public ArrayList<RankView> getListRankView(){
		ArrayList<RankView> listRankView = null;
		String query = "SELECT * FROM " +TABLE_NAME+ " ORDER BY score DESC, numberKill DESC" ;
		Cursor cursor = RankViewDAO._database.rawQuery(query, null);
		
		if(cursor.moveToFirst()){
			listRankView = new ArrayList<RankView>();
			do {
				if(listRankView.size() >= 10) break;
				RankView rankView = new RankView(cursor.getString(1), cursor.getLong(2), cursor.getInt(3));
				rankView.setId(cursor.getString(0));
				listRankView.add(rankView);
			} while (cursor.moveToNext());
		}
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		delete();
		if(listRankView != null){
			for (int i = 0; i < listRankView.size(); i++) {
				RankView rankView = listRankView.get(i);
				insert(rankView);
			}
		}
		_database.close();	
		return listRankView;
	}
	
	public RankView getBestRankView(){
		RankView bestRankView = null; 
		String query = "SELECT * FROM " +TABLE_NAME+ " ORDER BY score DESC, numberKill DESC LIMIT 1" ;
		Cursor cursor = RankViewDAO._database.rawQuery(query, null);
		
		if(cursor.moveToFirst()){
			bestRankView = new RankView(cursor.getString(1), cursor.getLong(2), cursor.getInt(3));
		}
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		_database.close();	
		return bestRankView;
	}
	
	public void delete(){
		String sql = "DELETE FROM " +TABLE_NAME+ ";";
		
		_database.execSQL(sql);
		
	}

}
